//
//  CLPageViewController.m
//  CLBrowser
//
//  Created by 梁慧聪 on 2022/7/22.
//

#import "CLPageViewController.h"

// 左边上一页点击区域
#define LeftWidth (SCREENWIDTH / 3)

// 右边下一页点击区域
#define RightWidth (SCREENWIDTH / 3)

@interface CLPageViewController () <UIGestureRecognizerDelegate>
@property (nonatomic, strong) UITapGestureRecognizer *customTapGestureRecognizer;
@end

@implementation CLPageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tapGestureRecognizerEnabled = NO;
    self.customTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(touchTap:)];
    [self.view addGestureRecognizer:self.customTapGestureRecognizer];
}


- (void)touchTap:(UITapGestureRecognizer *)tap {
    CGPoint touchPoint = [tap locationInView:self.view];
    
    if (touchPoint.x < LeftWidth) { // 左边
        
        if ([self.aDelegate respondsToSelector:@selector(pageViewController:getViewControllerBefore:)]) {
            [self.aDelegate pageViewController:self getViewControllerBefore:self.viewControllers.firstObject];
        }
        
    }else if (touchPoint.x > (SCREENWIDTH - RightWidth)) { // 右边
        if ([self.aDelegate respondsToSelector:@selector(pageViewController:getViewControllerAfter:)]) {
            [self.aDelegate pageViewController:self getViewControllerAfter:self.viewControllers.firstObject];
        }
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer {
    if ([gestureRecognizer isKindOfClass:[UITapGestureRecognizer class]] && [gestureRecognizer isEqual:self.customTapGestureRecognizer]) {
        CGPoint touchPoint = [self.customTapGestureRecognizer locationInView:self.view];
        if (touchPoint.x > LeftWidth && touchPoint.x < (SCREENWIDTH - RightWidth)) {
            return YES;
        }
    }
    return NO;
}
@end
